package trong.gold.soft.com.view.game;

import trong.gold.soft.com.main.*;
import trong.gold.soft.com.model.Record;
import trong.gold.soft.com.model.Resources;
import trong.gold.soft.com.model.font.VietnameseFont;
import trong.gold.soft.com.view.menuSlGame;

import javax.microedition.lcdui.Canvas;
import javax.microedition.lcdui.Font;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;

 
public class viewUser extends Canvas implements Runnable
{	
	static final char[] KEY_NUM1_CHARS = {'.', '?', '!', '1'};
	static final char[] KEY_NUM2_CHARS = {'a', 'b', 'c', '2'};
	static final char[] KEY_NUM3_CHARS = {'d', 'e', 'f', '3'};
	static final char[] KEY_NUM4_CHARS = {'g', 'h', 'i', '4'};
	static final char[] KEY_NUM5_CHARS = {'j', 'k', 'l', '5'};
	static final char[] KEY_NUM6_CHARS = {'m', 'n', 'o', '6'};
	static final char[] KEY_NUM7_CHARS = {'p', 'q', 'r', 's', '7'};
	static final char[] KEY_NUM8_CHARS = {'t', 'u', 'v', '8'};
	static final char[] KEY_NUM9_CHARS = {'w', 'x', 'y', 'z', '9'};
	static final char[] KEY_NUM0_CHARS = {' ', '0'};
 
	int clearKeyCode = Integer.MIN_VALUE;
	private int width,height;
	StringBuffer currentText = new StringBuffer();
	String currentString = new String();
 
	int lastPressedKey = Integer.MIN_VALUE;
	int currentKeyStep = 0;
 
	Font inputFont = null;
	int inputWidth = 0;
	int inputHeight = 0;
	int inputTranslationX;
 
	long lastKeyTimestamp = 0;
	long maxKeyDelay = 500L;
 
	int caretIndex = 0;
	int caretLeft = 0;
	boolean caretBlinkOn = true;
	long caretBlinkDelay = 500L;
	long lastCaretBlink = 0;
 
	boolean goToNextChar = true;
	private mTetrisMidlet midlet;
	private Image ktin;
	public viewUser(mTetrisMidlet midlet)
	{
		this.midlet = midlet;
		ktin = Resources.imgoto;
		this.setFullScreenMode(true);
		new Thread(this).start();
		inputTranslationX = this.getWidth()/2 - ktin.getWidth()/2 + 20;
		inputFont = Font.getDefaultFont();
 
		inputWidth = getWidth();
 
		inputHeight = inputFont.getHeight();
	}
 
	public char[] getChars(int key)
	{
		switch(key)
		{
		case Canvas.KEY_NUM1: return KEY_NUM1_CHARS;
		case Canvas.KEY_NUM2: return KEY_NUM2_CHARS;
		case Canvas.KEY_NUM3: return KEY_NUM3_CHARS;
		case Canvas.KEY_NUM4: return KEY_NUM4_CHARS;
		case Canvas.KEY_NUM5: return KEY_NUM5_CHARS;
		case Canvas.KEY_NUM6: return KEY_NUM6_CHARS;
		case Canvas.KEY_NUM7: return KEY_NUM7_CHARS;
		case Canvas.KEY_NUM8: return KEY_NUM8_CHARS;
		case Canvas.KEY_NUM9: return KEY_NUM9_CHARS;
		case Canvas.KEY_NUM0: return KEY_NUM0_CHARS;
		}
		return null;
	}
	void clearChar()
	{
		if(currentText.length() > 0 && caretIndex > 0)
		{
			caretIndex--;
 
			currentText.deleteCharAt(caretIndex);
 
			currentString = currentText.toString();
		}
	}
	//thay doi vi tri
	void updateCaretPosition()
	{
		caretLeft = inputFont.substringWidth(currentString, 0, caretIndex);
 
		if(caretLeft + inputTranslationX < 0)
		{
			inputTranslationX = - caretLeft;
		}
		else if(caretLeft + inputTranslationX > inputWidth)
		{
			inputTranslationX = inputWidth - caretLeft;
		}
	}
	public void keyPressed(int key)
	{
		switch(key){
			case -7:
				clearChar();
				 
				updateCaretPosition();
	 
				goToNextChar = true;
				break;
			case  KEY_NUM0:
				writeKeyPressed(key);
				break;
			case  KEY_NUM1:
				writeKeyPressed(key);
				break;
			case  KEY_NUM2:
				writeKeyPressed(key);
				break;
			case  KEY_NUM3:
				writeKeyPressed(key);
				break;
			case  KEY_NUM4:
				writeKeyPressed(key);
				break;
			case  KEY_NUM5:
				writeKeyPressed(key);
				break;
			case  KEY_NUM6:
				writeKeyPressed(key);
				break;
			case  KEY_NUM7:
				writeKeyPressed(key);
				break;
			case  KEY_NUM8:
				writeKeyPressed(key);
				break;
			case  KEY_NUM9:
				writeKeyPressed(key);
				break;
			case -3:
				if(caretIndex > 0)
				{
					caretIndex--;
	 
					updateCaretPosition();
	 
					goToNextChar = true;
				}
				break;
			case -4:
				if(caretIndex < currentText.length())
				{
					if(goToNextChar)
						caretIndex++;
	 
					updateCaretPosition();
	 
					goToNextChar = true;
				}
				break;
			case -6:
				if(currentString.length()>0)
					process();
				break;
				
		}

	}
	public int getvtname(){
		return TetrisClassic.getvtname();
	}
	private void process(){
		
		for(int i=Resources.name.length - 1;i>=getvtname() + 1;i--){
			Resources.name[i]= Resources.name[i-1];
		}
		Resources.name[getvtname()]= currentString;
		Record.open();
		Record.save();
		Record.close();
		setnull();
		Runtime.getRuntime().gc();
		midlet.switchState(this, new menuSlGame(midlet));
	}
	private void writeKeyPressed(int key)
	{
		if(currentString.length()==8){
			return;
		}
		if(goToNextChar || key != lastPressedKey)
		{
			goToNextChar = true;
 
			lastPressedKey = key;
 
			currentKeyStep = 0;
		}
		else
		{
			currentKeyStep++;
		}
 
		char[] chars = getChars(key);
 
		if(chars != null)
		{
			if(currentKeyStep >= chars.length)
			{
				currentKeyStep -= chars.length;
			}
			if(goToNextChar)
			{
				currentText.insert(caretIndex, chars[currentKeyStep]);
 
				caretIndex++;
			}
			else
			{
				currentText.setCharAt(caretIndex - 1, chars[currentKeyStep]);
			}
			currentString = currentText.toString();
 
			updateCaretPosition();
 
			lastKeyTimestamp = System.currentTimeMillis();
 
			goToNextChar = false;
		}
	}
 
	public void checkTimestamps()
	{
		long currentTime = System.currentTimeMillis();
 
		if(lastCaretBlink + caretBlinkDelay < currentTime)
		{
			caretBlinkOn = !caretBlinkOn;
 
			lastCaretBlink = currentTime;
		}
 
		if(!goToNextChar && lastKeyTimestamp + maxKeyDelay < currentTime)
		{
			goToNextChar = true;
		}
	}
 
	public void run()
	{
		while(true)
		{
			checkTimestamps();
 
			repaint();
 
			try
			{
				synchronized(this)
				{
					wait(50L);
				}
			}
			catch(Exception e)
			{
				
			}
		}
	}
 
	public void paint(Graphics g)
	{
		width = getWidth();
		height = getHeight();
		
		g.setFont(inputFont);
		for(int i=0;i<=this.getWidth()/44;i++){
			g.drawImage(Resources.backgr, i*Resources.backgr.getWidth(), 0, Resources.TOP_LEFT_ANCHOR);
		}
		g.setColor(0xd8fbb6);
		g.fillRect(0, this.getHeight() - 25, this.getWidth(), 25);
		g.drawImage(Resources.backImage, this.getWidth() - 5, this.getHeight(), Graphics.BOTTOM | Graphics.RIGHT);
		g.drawImage(Resources.chonImg, 5, this.getHeight(), Graphics.BOTTOM | Graphics.LEFT);
		g.drawImage(Resources.imgicon, this.getWidth()/2 - Resources.imgicon.getWidth()/2, 10, Resources.TOP_LEFT_ANCHOR);
		g.drawImage(ktin, this.getWidth()/2 - ktin.getWidth()/2, this.getHeight()/5, Resources.TOP_LEFT_ANCHOR);
		VietnameseFont.BlackBold.DrawString(g, "Điểm cao",this.getWidth()/2, this.getHeight()/5 + VietnameseFont.BlackBold.getHeight(), Graphics.HCENTER);
		VietnameseFont.BlackBold.DrawString(g, "Nhập tên người chơi",this.getWidth()/2, this.getHeight()/5 + 4*VietnameseFont.BlackBold.getHeight(), Graphics.HCENTER);
	
		g.setColor(0xffffff);
		g.fillRect(width/2 - ktin.getWidth()/2 + 20, this.getHeight()/5 + 5*VietnameseFont.BlackBold.getHeight(),  ktin.getWidth() - 40, 2*VietnameseFont.BlackBold.getHeight());
		
		g.setColor(0x000000);
		g.translate(inputTranslationX, this.getHeight()/5 + 5*VietnameseFont.BlackBold.getHeight());
 
		g.drawString(currentString, 0, 0, Graphics.LEFT | Graphics.TOP);
		if(caretBlinkOn && goToNextChar)
		{
			g.drawLine(caretLeft, 0, caretLeft, inputHeight);
		}
		g.translate(- inputTranslationX, 0);
	}
	private void setnull(){
		currentString=null;
		inputFont=null;
		ktin=null;
	}
}

